<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<link rel="stylesheet" media="screen" href="${ctx}/css/Tree.css"/>

<div class="tabs side grid_4 leading">
    <ul class="clearfix" id="left_level"></ul>
    <section>
        <section class="clearfix" style="display: block;">

            <div class="grid_12 alpha">
                <div id="tree_view"></div>
            </div>
        </section>
    </section>
</div>

<script type="text/javascript" src="${ctx}/js/Tree.js"></script>
<script type="text/javascript">
    var mTreeViews;
    $(document).ready(function() {
        //getLeftLevels(null);
        setTimeout("init()", 600);//0.6秒后执行init()
    });

    function init(){
        getLeftLevels(null);   
    }

    //得到左边的结点
    function getLeftLevels() {
        $.getJSON("${ctx}/ajax/organization-children.xhtml", function(children) {
            if (children != null && children.length != 0) {
                var leftLevelStr = "";
                var firstId = 0;
                $.each(children, function(i, child) {
                    //alert(child.id+":"+child.fullName)
                    if (i == 0) {
                        firstId = child.id;
                        $("#default_organization_id").val(firstId);
                        nodeClick(firstId);
                        leftLevelStr += '<li><a class="current left_level_a" style="cursor:pointer;" onclick="onLeftNodeClick(' + child.id + ', this)">' + child.fullName + '</a></li>';
                    } else {
                        leftLevelStr += '<li><a class="left_level_a" style="cursor:pointer;" onclick="onLeftNodeClick(' + child.id + ', this)">' + child.fullName + '</a></li>';
                    }
                });
                $("#left_level").html("");
                $("#left_level").append(leftLevelStr);
                getChildrenById(firstId);
                mTreeViews = new Array(children.length);
            }
        });
    }

    function onTabChanage(obj) {
        $(".left_level_a").each(function() {
            //alert($(this).text())
            $(this).removeClass("current");
        });
        $(obj).addClass("current")
    }

    //单击左边的结点(分公司)
    function onLeftNodeClick(id, obj) {
        try{
            onTopOrganizationClick(id, obj);
        }catch(e){
        }

        nodeClick(id);
        onTabChanage(obj);
        $("#default_organization_id").val(id);

        var tree_view = null;
        for (var i = 0; i < mTreeViews.length; i++) {
            var t = mTreeViews[i];
            if (t != null) {
                var treeId = t.id;
                if (treeId == "treeview" + id) {
                    tree_view = t;
                    break;
                }
            }
        }
        $("#tree_view").html("");
        if (tree_view == null) {
            getChildrenById(id);
        } else {
            tree_view.create(document.getElementById("tree_view"));
        }
    }

    function getChildrenById(currentNode) {
        var paramsStr = (typeof currentNode == "number") ? "?parentId=" + currentNode : "?parentId=" + currentNode.id
        $.getJSON("${ctx}/ajax/organization-children.xhtml" + paramsStr, function(children) {
            if (children != null && children.length != 0) {
                if (typeof currentNode == "number") {
                    var tree_view = new treeview("treeview" + currentNode, "tree", true, false);
                    $.each(children, function(i, child) {
                        var n = new node(child.fullName, child.fullName, "", null, null, null, child.id);
                        tree_view.add(n);
                        getChildrenById(n);
                    });
                    $("#tree_view").html("");
                    tree_view.create(document.getElementById("tree_view"));
                    mTreeViews.push(tree_view);
                } else {
                    $.each(children, function(i, child) {
                        var n = new node(child.fullName, child.fullName, "", null, null, null, child.id);
                        currentNode.add(n);
                        getChildrenById(n);
                    });
                }
            }
        });
    }

    //得到左边当前部门对应的列表
    function onNodeClick(sender){
        $("#default_organization_id").val(sender.id);
        nodeClick(sender.id);
    }

    function nodeClick(organizationId){
        var url = "${ctx}${listAction}";
        if(url == null || url == ""){
            try{
                findDuty(organizationId);
            }catch(e){}
            return;
        }
        <s:if test="#request.module != null && #request.module.equals('outing_train_list')">
        	findDuty(organizationId);
        </s:if>
        var data = {"organizationId":organizationId};
        //refreshListContent(url, data);
        $.ajax(url, {
            type: "POST",
            //data:{"organizationId":organizationId},
            data:data,
            dataType:"html",
            cache:false,
            success:function(a) {
                var obj = $("#list_content");
                obj.html("");
                obj.append(a);
            },
            complete:function() {
            }
        });
    }
</script>
    